package com.huike.service;

import com.huike.dao.FriendDAO;
import com.huike.dao.SysCustomDAO;
import com.huike.dao.UserDAO;
import com.huike.po.Friend;
import com.huike.po.FriendShip;
import com.huike.po.SystemCustom;
import com.huike.po.UserDetail;
import com.huike.pojo.FriendCustomBean;
import com.huike.pojo.MsgUpdateObject;
import com.huike.pojo.SystemCustomObject;
import com.huike.websocket.ChatWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class FriendServiceImpl implements FriendService {
    @Autowired
    private FriendDAO friendDAO;
    @Autowired
    private SysCustomDAO sysCustomDAO;
    @Autowired
    private UserDAO userDAO;

    @Autowired
    private MsgUpdateObject msgUpdateObject;
    @Autowired
    private ChatWebSocketHandler webSocket;

    @Autowired
    private SystemCustom systemCustom;
    @Autowired
    private SystemCustomObject systemCustomObject;


    @Override
    public boolean handleSubscribe(String account, String friendAccount, String type) {
        if (type.equals("addSubscribe")) {
            return friendDAO.addSubscribe(account,friendAccount);
        }
        else if (type.equals("cancelSubscribe")) {
            return friendDAO.cancelSubscribe(account,friendAccount);
        }
        return false;
    }

    @Override
    public boolean handleFriend(FriendCustomBean friendRequest, String type) {
        // 插入好友相关的系统通知记录
        switch (type) {
            case "applyFriend":
                systemCustom.setSctype_id(1);
                systemCustomObject.setSysCustomContent("applyFriend");
                systemCustomObject.setReservedFields("");
                break;
            case "agreeApplyFriend":
                systemCustom.setSctype_id(2);
                systemCustomObject.setSysCustomContent("agreeApplyFriend");
                sysCustomDAO.updateSysCustomToBeHandled(friendRequest.getHandledSysId(), "您已同意了对方的好友申请");
                break;
            case "refuseApplyFriend":
                systemCustom.setSctype_id(3);
                systemCustomObject.setSysCustomContent("refuseApplyFriend");
                systemCustomObject.setReservedFields("");
                sysCustomDAO.updateSysCustomToBeHandled(friendRequest.getHandledSysId(), "您已拒绝了对方的好友申请");
                break;
        }
        // 插入好友相关的系统通知
        systemCustom.setSyscustom_from(friendRequest.getAccountFrom());
        systemCustom.setSyscustom_to(friendRequest.getAccountTo());
        sysCustomDAO.insertFriendSysCustom(systemCustom);

        if (type.equals("agreeApplyFriend")){
            // A同意B的好友申请，B前端实时更新好友记录
            Friend friend = friendDAO.selectFriendRelation(friendRequest.getAccountTo(),friendRequest.getAccountFrom());
            systemCustomObject.setReservedFields(friend);
        }
        // 判断接收方在线状态
        int onlineNum = userDAO.selectOnlineNumByAccount(friendRequest.getAccountTo());
        if( onlineNum >0) {
            // 接收方在线
            UserDetail userFrom = userDAO.selectUserDetailByAccount(friendRequest.getAccountFrom());
            systemCustomObject.setSysCustomType("FriendCustom");
            systemCustomObject.setSysCustomFrom(userFrom);
            systemCustomObject.setSysCustomTime(friendRequest.getSendTime());
            // 调用webSocket,发送好友相关系统通知实体
            webSocket.sendSysCustomToUser(systemCustomObject,friendRequest.getAccountTo());
        }
        return true;
    }

    @Override
    public Friend searchFriendRelation(String account, String friendAccount) {
        Friend friend  = friendDAO.selectFriendRelation(account,friendAccount);
        return friend;
    }

    //修改好友备注
    @Override
    public boolean updateFriendAlias(FriendShip friend) {
        if(friendDAO.updateFriendAlias(friend))
            return true;
        else
            return false;
    }

    //修改好友标签
    @Override
    public boolean updateFriendTags(FriendShip friend) {
        if(friendDAO.updateFriendTags(friend))
            return true;
        else
            return false;
    }
    //好友删除
    @Override
    public boolean deleteFriend(String account ,String friendAccount) {
        friendDAO.deleteFriend(account ,friendAccount);
        // 判断被删除好友在线状态
        int onlineNum = userDAO.selectOnlineNumByAccount(friendAccount);
        if( onlineNum >0) {
            //设置状态更新对象
            msgUpdateObject.setUpdateType("UpdateFriendList");
            msgUpdateObject.setUpdateContent("delete");
            msgUpdateObject.setUpdateFrom(account);
            //调用ChatWebSocketHandler类中的自定义方法，修改好友的用户在线状态
            webSocket.sendUpdateToUser(msgUpdateObject,friendAccount);
        }
        return true;
    }

    //好友拉黑
    @Override
    public boolean setFriendBlacked(FriendShip friend) {
        if(friendDAO.setFriendBlacked(friend))
            return true;
        else
            return false;
    }

    //取消好友拉黑
    @Override
    public boolean cancelFriendBlacked(FriendShip friend) {
        if(friendDAO.cancelFriendBlacked(friend))
            return true;
        else
            return false;
    }

}
